<template>
  <div class="base-container">
    <!-- <t-form ref="form" :data="queryParams" :label-width="80" @submit="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin: 10px 0px">
        <t-col :span="11" class="query-form-inline" >
          <t-form-item label="类别名称" name="name">
            <t-input
              v-model="queryParams.name"
              clearable
              placeholder="类别名称"
            />
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form> -->
    <div class="btn-group-inline">
      <t-button @click="dialog.header='新增计划';dialog.objId=0;dialog.parentId=0;dialog.open = true;" variant="outline" theme="primary">
        <t-icon name="add" slot="icon"/>
        新增
      </t-button>
      <t-button @click="onExpandAllToggle" variant="outline" theme="primary">
        <t-icon name="swap" slot="icon"/>
        展开/折叠
      </t-button>
    </div>
    <t-enhanced-table
      ref="table"
      rowKey="id"
      :data="data"
      :columns="columns"
      hover
      :loading="dataLoading"
      :expandAll="expandAll"
      :expandedTreeNodes.sync="expandedTreeNodes"
      :tree="{ childrenKey: 'children', expandTreeNodeOnClick: true}"
    >
      <template #categoryType="{ row }">
        <t-tag v-if="row.categoryType==='1'" theme="primary" variant="light">计划</t-tag>
        <t-tag v-else-if="row.categoryType==='2'" theme="success" variant="light">专题</t-tag>
        <t-tag v-else-if="row.categoryType==='3'" theme="warning" variant="light">方向</t-tag>
      </template>
      <template #startDate="{ row }">
        <template v-if="row.categoryType === '1'">
          {{ row.startDate }}
        </template>
      </template>
      <template #endDate="{ row }">
        <template v-if="row.categoryType === '1'">
          {{ row.endDate }}
        </template>
      </template>
      <template #enable="{ row }">
        <t-tag v-if="row.enable === '1'" theme="success" variant="light">启用</t-tag>
        <t-tag v-else theme="danger" variant="light">停用</t-tag>
      </template>
      <template #op="{ row }">
        <t-button @click.stop="dialog.header='修改计划';dialog.objId=row.id;dialog.row=row;dialog.parentId=0;dialog.open = true;" size="small" variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon"/>
          修改
        </t-button>
        <t-button v-if="row.categoryType!=='3'"  size="small" @click.stop="dialog.header='添加计划';dialog.objId=0;dialog.parentId=row.id;dialog.open = true;" variant="text" class="t-button-link">
          <t-icon name="add" slot="icon"/>
          新增
        </t-button>
        <t-button theme="danger" @click.stop="handleDel(row.id)"  size="small" variant="text" class="t-button-link">
          <t-icon name="delete" slot="icon"/>
          删除
        </t-button>
      </template>
    </t-enhanced-table>
    <add-edit-plan @reload="getList()"  :header="dialog.header" :objId="dialog.objId" :parentId="dialog.parentId" :visible.sync="dialog.open" v-if="dialog.open"></add-edit-plan>
  </div>
</template>

<script>
import AddEditPlan from './components/add-edit-plan.vue'

export default {
  name: "plan-category",
  components:{
    AddEditPlan
  },
  data(){
    return {
      expandAll: false,
      expandedTreeNodes: [],
      fileLists: [],
      columns:[
        // {
        //   colKey: "id",
        //   title: "计划id",
        //   align: "center"
        // },
        {
          colKey: "name",
          title: "类别名称",
          width: 200
        },
        {
          colKey: "categoryType",
          title: "类型",
          align: "center",
          width: 100
        },
        {
          colKey: "startDate",
          title: "申报开始时间",
          align: "center",
          width: 120
        },
        {
          colKey: "endDate",
          title: "申报截止时间",
          align: "center",
          width: 120
        },
        // {
        //   colKey: "spStartDate",
        //   title: "申报审批开始时间",
        //   align: "center",
        //   width: 150
        // },
        // {
        //   colKey: "spEndDate",
        //   title: "申报审批截止时间",
        //   align: "center",
        //   width: 150
        // },
        {
          colKey: "dgStartDate",
          title: "大纲开始时间",
          align: "center",
          width: 120
        },
        {
          colKey: "dgEndDate",
          title: "大纲截止时间",
          align: "center",
          width: 120
        },
        {
          colKey: "rwsStartDate",
          title: "任务书开始时间",
          align: "center",
          width: 130
        },
        {
          colKey: "rwsEndDate",
          title: "任务书截止时间",
          align: "center",
          width: 130
        },
        {
          colKey: "ysStartDate",
          title: "验收开始时间",
          align: "center",
          width: 120
        },
        {
          colKey: "ysEndDate",
          title: "验收截止时间",
          align: "center",
          width: 120
        },
        {
          colKey: "enable",
          title: "是否启用",
          align: "center",
          width: 100
        },
        // {
        //   colKey: "note",
        //   title: "描述",
        //   align: "center",
        //   ellipsis: true
        // },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          fixed: 'right',
        },
      ],
      dialog:{
        open:false,
        header:"",
        objId:0,
        row: {}
      },
      data:[],
      dataLoading: false,
      queryParams: {
        guideName:"",
        guideYear:"",
        note: "",
      },
    }
  },
  created() {
    this.getList();
  },
  methods:{
    onExpandAllToggle() {
      this.expandAll = !this.expandAll;
      if (this.expandAll){
        this.$refs.table.expandAll();
      }else{
        this.$refs.table.foldAll();
      }
    },
    getList(){
      this.dataLoading = true;
      this.$api.kjxm.project.planCategoryList(this.queryParams).then(
        (res) => {
          this.data = this.handleTree(res.data, "id");
          // this.$refs.table?.resetData(this.data);
          this.dataLoading = false;
        }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    handleDel(id){
      const confirmDia = this.$dialog.confirm({
        header:"系统提示",
        body:`是否确认删除编号为"${id}"的数据项？`,
        theme: "warning",
        onConfirm: ({ e }) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          this.$api.kjxm.project.planCategoryDel(id).then(
            (res) => {
              this.getList();
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });
        },
      })
    }
  }
}
</script>

<style scoped lang="less">
.op_groups{
  display: flex;

}
</style>
